package com.aiju.common.http;

import android.content.Context;

import com.aiju.app.callback.HttpCallback;
import com.aiju.utils.Util;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author: 佐为
 * @date: 2017-03-23 15:53
 * @version: V1.0
 * @Description: 使用第三方库OkHttp做网络请求
 */
public class OkHttp implements AiJuHttp {

    public final static int CONNECT_TIMEOUT =60;
    public final static int READ_TIMEOUT=100;
    public final static int WRITE_TIMEOUT=60;

    private OkHttpClient okHttpClient;

    @Override
    public void init(Context context) {
        okHttpClient = new OkHttpClient.Builder()
                .readTimeout(READ_TIMEOUT,TimeUnit.SECONDS)//设置读取超时时间
                .writeTimeout(WRITE_TIMEOUT,TimeUnit.SECONDS)//设置写的超时时间
                .connectTimeout(CONNECT_TIMEOUT,TimeUnit.SECONDS)//设置连接超时时间
                .build();
    }

    @Override
    public void requestGet(String url, final HttpCallback callback) {
        final okhttp3.Request request = new okhttp3.Request.Builder()
                .url(url)
                .tag(url)
                .build();

        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (callback != null) {
                    callback.onSuccess(response.body().string());
                }
            }

            @Override
            public void onFailure(Call call, IOException e) {
                if (callback != null) {
                    callback.onFailure(4004, "网络错误");
                }
            }
        });
    }

    @Override
    public void requestPost(String url, Map<String, String> parameters, final HttpCallback callback) {
        FormBody.Builder builder = new FormBody.Builder();
        if (!Util.isEmpty(parameters)) {
            for (Map.Entry<String, String> entry : parameters.entrySet()) {
                builder.add(entry.getKey(), entry.getValue());
            }
        }

        RequestBody requestBody = builder.build();
        final okhttp3.Request request = new okhttp3.Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (callback != null) {
                    callback.onSuccess(response.body().string());
                }
            }

            @Override
            public void onFailure(Call call, IOException e) {
                if (callback != null) {
                    callback.onFailure(4004, "网络错误");
                }
            }
        });
    }

    @Override
    public void cancel(String tag) {
        okHttpClient.dispatcher().cancelAll();
    }

}
